<?php

declare(strict_types=1);

namespace app\common\service;

use think\facade\Filesystem;
use OSS\OssClient;
use think\App;
use think\facade\Env;
use app\data\util\Upload;

/**
*
 * @title 上传组件服务
 * @package app\common\service
 * @class   UploadService
 * @author  xx
 * @date   2024-02-17
 */
class UploadService
{
    private $accessId;
    private $accessSecret;
    private $bucket;
    private $endpoint;



    public function __construct()
    {
        $this->accessId           = env('OSS_OSS_ACCESS_ID');
        $this->accessSecret       = env('OSS_OSS_ACCESS_SECRET');
        $this->bucket             = env('OSS_OSS_BUCKET');
        $this->endpoint           = env('OSS_OSS_ENDPOINT');
    }

    /**
     * 文件上传到阿里云服务器香港
     * @date 2024-2-17
     */
     public function uploads($filename,$ossDir){
         try{
             $ossClient = new OssClient($this->accessId, $this->accessSecret, $this->endpoint);
             return $ossClient->uploadFile($this->bucket,$ossDir,$filename);
         } catch(OssException $e) {
             p($e->getMessage() . "\n");
             return;
         }
     }



    /**
     * @param $service
     * @param $allowExt
     * @param $maxSize
     * @return mixed
     * @desc  上传excel文件
     */
    function uploadExcelData($service, $allowExt = [], $maxSize = '15728640')
    {

        $uploadPathImport = UPLOAD_FILE_DOMAIN_NAME . '/excel/' . date('Ym/d/');
        $path = FCPATH . $uploadPathImport;
        if (!is_dir($path)) mkdir($path, 0777, true);
        $load = new Upload('file', $uploadPathImport, $maxSize, $allowExt ? $allowExt : ['xls', 'xlsx', 'xlsm', 'xsd', 'csv']);
        return $load->uploadExcel($service->uploadName);
    }














}
